import React from 'react';
import { Spin } from 'antd';
import { isEqual, isEmpty } from 'lodash';
import {WeaTools, WeaErrorPage, WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const { ls } = WeaTools;
import EHeader from '../common/EHeader';
import FooterMore from "../common/FooterMore";
import { formatDate, NoRightCom } from '../../util/common';
import MeetingCalendarTemplate from './MeetingCalendar';
//元素组件
class MeetingCalendar extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            edata: null,
            refresh: false,
            weekStartDay:null,
        }
        this.handleEData = this.handleEData.bind(this);
        this.handleRefresh = this.handleRefresh.bind(this);
        this.handleDelete = this.handleDelete.bind(this);
    }
    handleDelete(){
        this.setState({
            edata: null,
        });
        ls.set('edata-'+this.props.config.params.eid, null);
    }
    handleRefresh(){
        this.setState({
            refresh: true,
        });
        this.handleEData(this.props.config.params);
    }
    handleEData(params){
        const { eid, api } = params;
        WeaTools.callApi(api || '/api/portal/element/meetingCalendar', 'POST', {eid}).then(result=> {
            WeaTools.callApi('/api/meeting/calendar/getData', 'POST', {viewType: '3',
                selectdate: this.refs.meetingCalendarTemplate && this.refs.meetingCalendarTemplate.state.firstDay ?
                    this.refs.meetingCalendarTemplate.state.firstDay: formatDate(new Date(), "yyyy-MM-dd") ,
                meetingType: result.meetingtype }).then(data=> {
                ls.set('edata-'+eid, data);
                this.setState({
                    edata: data,
                    refresh: false,
                    weekStartDay:data.weekStartDay
                });
            });
        });
    }
    componentDidMount(){
        const { config = {} } = this.props;
        const { params = {} } = config;
        const { eid } = params;
        const edata = ls.getJSONObj(`edata-${eid}`);
        if (config.islsdata && edata) {
            this.setState({ edata });
        } else {
            this.handleEData(params);
        }
    }
    componentWillReceiveProps(nextProps){
        const { item, params, isHasRight } = this.props.config;
        const config = { item, params, isHasRight };
        const _config = { 
            item: nextProps.config.item, 
            params: nextProps.config.params, 
            isHasRight: nextProps.config.isHasRight,
        };
        if(!isEqual(config, _config) || nextProps.config.isRender){
            this.handleEData(nextProps.config.params);
        }
    }
    shouldComponentUpdate(nextProps, nextState){
        const { item, params, isHasRight } = this.props.config;
        const config = { item, params, isHasRight };
        const _config = { 
            item: nextProps.config.item, 
            params: nextProps.config.params, 
            isHasRight: nextProps.config.isHasRight,
        };
        return !isEqual(this.state, nextState) || !isEqual(config, _config)
    }
    render() {
        const { edata, refresh ,weekStartDay} = this.state;
        const { config, fontSizeMp } = this.props;
        if(!config || !edata) return <div/>;
        const { isHasRight, item, params, layoutid } = config;
        const { eid, ebaseid, content, header, contentview, style } = item;
        const { style: style1= {} } = contentview;
        let EContentHtml = <div/>;
        if(isHasRight){
            if(!isEmpty(edata)) EContentHtml = <MeetingCalendarTemplate weekStartDay={weekStartDay} ecId={`${this && this.props && this.props.ecId || ''}_MeetingCalendarTemplate@59hpz5`} ref={'meetingCalendarTemplate'} handleRefresh={this.handleRefresh} eid={eid} data={edata} hpid={params.hpid} layoutid={layoutid} subCompanyId={params.subCompanyId} eid={eid}/>;
        }else{
            EContentHtml = <NoRightCom ecId={`${this && this.props && this.props.ecId || ''}_NoRightCom@ke1ep5`}/>
        }
        return <div className="item"
                    style={style}
                    id={`item_${eid}`} 
                    data-eid={eid} data-ebaseid={ebaseid} 
                    data-needRefresh={item.needRefresh} 
                    data-cornerTop={item.cornerTop} 
                    data-cornerTopRadian={item.cornerTopRadian}
                    data-cornerBottom={item.cornerBottom} 
                    data-cornerBottomRadian={item.cornerBottomRadian}>
                    <EHeader ecId={`${this && this.props && this.props.ecId || ''}_EHeader@vhdf60`} config={config} handleRefresh={this.handleRefresh} handleDelete={this.handleDelete} fontSizeMp/>
                    <div className="setting" id={`setting_${eid}`}></div>
                    <div className="content" id={`content_${eid}`} style={{width:'auto',_width:'100%'}}>
                        <div className="content_view" id={`content_view_id_${eid}`} style={{height: style1.height}}>
                            <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@6gw97a`} spinning={refresh}>{EContentHtml}</Spin>
                        </div>
                        <div style = {{textAlign:'right'}} id={`footer_${eid}`}><FooterMore ecId={`${this && this.props && this.props.ecId || ''}_FooterMore@zizph3`} config={this.props.config}/></div>
                    </div>
            </div>;
    }
}
class MyErrorHandler extends React.Component {
    render() {
        const hasErrorMsg = this.props.error && this.props.error !== "";
            return ( <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@8vqoaa`} msg = { hasErrorMsg ? this.props.error : getLabel(385218,"对不起，该页面异常，请联系管理员！")  }/>
        );
    }
}
MeetingCalendar = WeaTools.tryCatch(React, MyErrorHandler, { error: "" })(MeetingCalendar);
export default MeetingCalendar;



